//
//  SISSMyBrokerageViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2018/4/8.
//  Copyright © 2018年 优房网. All rights reserved.
//

#import "SISSMyBrokerageViewController.h"
#import "SISSMyBrokerageHeaderView.h"
#import "SISSMyBrokerageCell.h"
#import "SISSNetworkingManager+Mine.h"

@interface SISSMyBrokerageViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) UITableView *tableView;
@property (weak, nonatomic) SISSMyBrokerageHeaderView *headerView;

@property (assign, nonatomic) NSInteger pageNo;

///
@property (strong, nonatomic) NSMutableArray<SISSWalletIncomeDetailModel *> *incomeDetailArr;

@end

@implementation SISSMyBrokerageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.pageNo = 1;
    self.title = @"我的钱包";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(backItemAction)];
    
    [self requestWalletInfoData];
    [self requestWalletIncomeDetailInfoData];
    WeakSelf
    [kSISSRefresh prepareHeaderRefresh:self.tableView loadDataBlock:^{
        kSISSRefresh.refreshType = SISSRefressTypeUpDown;
        [weakSelf upDownLoadData];
    }];
    [kSISSRefresh prepareFooterRefresh:self.tableView reloadDataBlock:^{
        kSISSRefresh.refreshType = SISSRefressTypeReloadMore;
        [weakSelf reloadMoreData];
    }];
}


#pragma mark - 上下拉刷新加载方法
- (void)upDownLoadData {
    self.pageNo = 1;
    [self requestWalletIncomeDetailInfoData];
}

- (void)reloadMoreData {
    
    self.pageNo += 1;
    [self requestWalletIncomeDetailInfoData];
}

- (void)backItemAction {
    
    //    self.navigationController.navigationBar.backgroundColor = [UIColor de];
    //    如果不想让其他页面的导航栏变为透明 需要重置
    [self.navigationController.navigationBar setBackgroundImage:[ImageWithColor imageWithColor:[UIColor colorWithWhite:1 alpha:0.99]] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[ImageWithColor imageWithColor:[UIColor colorWithWhite:1 alpha:0.99]]];
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{
                                                                      NSForegroundColorAttributeName:COLOR(0x334A60),NSFontAttributeName:[UIFont systemFontOfSize:17]
                                                                      }];
    self.navigationController.navigationBar.tintColor = COLOR(0xB0C0D6);
    
    
    [self.navigationController popViewControllerAnimated:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    //设置导航栏背景图片为一个空的image，这样就透明了
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    //去掉透明后导航栏下边的黑边
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{
                                                                      NSForegroundColorAttributeName:COLOR(0xFFFFFF),NSFontAttributeName:[UIFont systemFontOfSize:17]
                                                                      }];
    self.navigationController.navigationBar.backgroundColor = COLOR(0x54CD78);
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
}


- (void)withdrawalsButtonClickAction {
    [self alertActionAlertWithTitle:@"提现" message:@"经纪人员不能主动提现，请联系机构文员使用机构帐户登录深圳市房地产中介协会综合服务系统（SISS）后台-交易通-佣金管理提现。" confirmTitle:@"知道了"];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    [tableView tableViewDisplayWithMessage:@"没有相关数据！" ifNecessaryForRowCount:self.incomeDetailArr.count];
    return self.incomeDetailArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SISSMyBrokerageCell *cell = [tableView dequeueReusableCellWithIdentifier:myBrokerageCellReuseID];
    cell.walletIncomeDetailModel = self.incomeDetailArr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//    UILabel *label = [UILabel yyr_labelWithText:@"收支明细" fontSize:17 textColor:MAIN_TEXT_COLOR  alignment:NSTextAlignmentCenter];
//    label.backgroundColor = [UIColor groupTableViewBackgroundColor];
//    return label;
//}
//
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
//    return 44;
//}

//- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//
//    CGFloat offset = scrollView.contentOffset.y;
//    //    NSLog(@"%@",@(offset));
//    CGRect rect = self.headerView.frame;
//
//    if (offset <= 0) {
//        rect.origin.y = offset;
//        rect.size.height = 140 - offset;
//        self.headerView.frame = rect;
//    }
//}

#pragma mark - 加载数据
- (void)requestWalletInfoData {
    
    [kNetworkManager requestPersonCenterWalletInfoWithSuccessBlock:^(SISSWalletInfoModel *walletInfoModel) {
        self.headerView.accountMoney = walletInfoModel.balance;
        self.headerView.accountFrozenMoney = walletInfoModel.freezeAmount;
    } failed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

- (void)requestWalletIncomeDetailInfoData {
    
    WeakSelf
    [kNetworkManager requestPersonCenterWalletIncomeDetailInfoWithPageNo:self.pageNo successBlock:^(SISSBasePagingModel *baseModel) {
        NSArray *incomeDetailArr = [NSArray yy_modelArrayWithClass:[SISSWalletIncomeDetailModel class] json:baseModel.data];
        
        if (weakSelf.incomeDetailArr.count != 0 && kSISSRefresh.refreshType == SISSRefressTypeUpDown) {
            
            [weakSelf.incomeDetailArr removeAllObjects];
        }
        
        for (int i = 0; i < incomeDetailArr.count; i++) {
            
            [weakSelf.incomeDetailArr addObject:incomeDetailArr[i]];
        }
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            
            [weakSelf.tableView reloadData];
            weakSelf.tableView.mj_footer.hidden = baseModel.total == 0;
            [weakSelf.tableView.mj_header endRefreshing];
            [weakSelf.tableView.mj_footer endRefreshing];
            if (weakSelf.incomeDetailArr.count >= baseModel.total) {
                //                weakSelf.tableView.mj_footer.hidden = YES;
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }];
    } failed:^(NSError *error) {
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];
        NSLog(@"%@",error);
    }];
}



- (void)setupSubviews {
    [super setupSubviews];
    
    SISSMyBrokerageHeaderView *headerView = [[SISSMyBrokerageHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 400 + kTopHeight)];
    [self.view addSubview:headerView];
    self.headerView = headerView;
    
    UILabel *label = [UILabel yyr_labelWithText:@"收支明细" fontSize:17 textColor:MAIN_TEXT_COLOR  alignment:NSTextAlignmentCenter];
    label.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:label];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [tableView registerClass:[SISSMyBrokerageCell class] forCellReuseIdentifier:myBrokerageCellReuseID];
    [self.view addSubview:tableView];
    self.tableView = tableView;

    CGFloat buttonHeight = 44;
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
//        make.height.mas_equalTo(160 + kTopHeight);
    }];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.headerView.mas_bottom);
        make.height.mas_equalTo(buttonHeight);
    }];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(label.mas_bottom);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-buttonHeight);
    }];
    
    UIButton *withdrawalsButton = [UIButton yyr_buttonWithTitle:@"提现" textColor:[UIColor whiteColor] andSize:17];
    [withdrawalsButton setBackgroundColor:COLOR(0x54CD78)];
    [withdrawalsButton addTarget:self action:@selector(withdrawalsButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:withdrawalsButton];
    
    [withdrawalsButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(buttonHeight);
        make.bottom.mas_equalTo(self.mas_bottomLayoutGuideTop);
    }];
}

- (void)initConstraint {
    [super initConstraint];
}

#pragma mark - 懒加载
- (NSMutableArray<SISSWalletIncomeDetailModel *> *)incomeDetailArr {
    if (!_incomeDetailArr) {
        _incomeDetailArr = [NSMutableArray array];
    }
    return _incomeDetailArr;
}

@end
